import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import comment from './api/comment'
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)
  let isLogin = localStorage.getItem("token")
  // next()
  if (to.meta.needLogin) { // 判断该路由是否需要登录权限

    if (isLogin) { // 判断是否已经登录
      let userObj = store.state.food.userObj
      console.log(userObj)
      console.log(JSON.stringify(userObj))
      // debugger
      if (typeof (userObj) === "object" && JSON.stringify(userObj) != "{}" && userObj != null) {
        //这里是已经登录并且有用户信息
        store.dispatch("food/checkVersion")
        next()

      } else {
        //没有用户信息去获取
        store.dispatch("food/getUserObj").then(res => {
          console.log(res.data["user"].username)
          store.dispatch("food/checkVersion")
          next()
        }).catch((error) => [

        ])

      }
    } else {
      Message("未登录，跳转到登录页面...")
      next({
        path: '/login',
        query: {
          redirect: to.fullPath
        } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }

  // determine whether the user has logged in
  //const hasToken = getToken()
  // if (hasToken) {
  //   if (to.path === '/login') {
  //     // if is logged in, redirect to the home page
  //     next({ path: '/' })
  //     NProgress.done()
  //   } else {
  //     const hasGetUserInfo = store.getters.name
  //     if (hasGetUserInfo) {
  //       next()
  //     } else {
  //       try {
  //         // get user info
  //         await store.dispatch('user/getInfo')

  //         next()
  //       } catch (error) {
  //         // remove token and go to login page to re-login
  //         await store.dispatch('user/resetToken')
  //         Message.error(error || 'Has Error')
  //         next(`/login?redirect=${to.path}`)
  //         NProgress.done()
  //       }
  //     }
  //   }
  // } else {
  //   /* has no token*/

  //   if (whiteList.indexOf(to.path) !== -1) {
  //     // in the free login whitelist, go directly
  //     next()
  //   } else {
  //     // other pages that do not have permission to access are redirected to the login page.
  //     next(`/login?redirect=${to.path}`)
  //     NProgress.done()
  //   }
  // }
})

router.afterEach((to, from) => {
  console.log("roytest1")
  console.log(to)
  console.log(from)
  let toPath = to.path
  let fromPath = from.path

  let tableNameTo = comment.getTableNameForPath(toPath)
  if (tableNameTo) {
    comment.createPoint({ tableName:tableNameTo, moduleName: "", pageName: toPath, eventName: "load", localTime: new Date().Format("yyyy-MM-dd hh:mm:ss.S") }).then(res => { })
  }
  console.log(tableNameFrom,from.name,from.path)
  let tableNameFrom = comment.getTableNameForPath(fromPath)
  if(tableNameFrom && from.name && from.path !== "/"){
    //这里就是离开路由了
    comment.createPoint({ tableName:tableNameFrom, moduleName: "", pageName: fromPath, eventName: "leave", localTime: new Date().Format("yyyy-MM-dd hh:mm:ss.S") }).then(res => { })
  }
  // finish progress bar
  NProgress.done()
})
